package cn.kgc.day05.service.impl;


import cn.kgc.day05.dao.ScoreDao;
import cn.kgc.day05.dao.impl.ScoreDaoImpl;
import cn.kgc.day05.entity.Score;
import cn.kgc.day05.http.ResponseResult;
import cn.kgc.day05.service.ScoreService;

import java.util.List;

/**
 * @author 课工场
 * @date 2024/10/11
 * @description   业务处理  不做数据库相关操作
 */
public class ScoreServiceImpl implements ScoreService {

    private ScoreDao scoreDao = new ScoreDaoImpl();


    @Override
    public List<Score> scoreList() {

        return  scoreDao.selectScoreList();
    }

    @Override
    public Boolean addScore(Score s) {

       Integer line =  scoreDao.insertScore(s);

        return line==1;
    }

    @Override
    public Score selectOne(Integer dbId) {

        Score score  =  scoreDao.selectScoreById(dbId);
        return score;
    }

    @Override
    public Boolean editScore(Score s) {
       Integer line =  scoreDao.editScore(s);
        return line==1;
    }

    @Override
    public Boolean deleteById(Integer id) {

       Integer line =  scoreDao.deleteById(id);
        return line==1;
    }

    @Override
    public ResponseResult scorePage(Integer page, Integer limit) {

         int start = (page-1)*limit;
         // 当前页数据
         List<Score> scoreList =   scoreDao.scorePage(start,limit);
         // 数据总条数  select  count(*)  from  score
         Long  total = scoreDao.selectTotal();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setCount(total);
        responseResult.setData(scoreList);
        return responseResult;
    }

    @Override
    public ResponseResult saveScore(Score scr) {

        ResponseResult responseResult = new ResponseResult();
        try {
            Integer line = scoreDao.insertScore(scr);
            responseResult.setCode(0);
            responseResult.setMsg("操作成功");
        }catch (Exception e){
            responseResult.setCode(110);
            responseResult.setMsg("操作失败");
        }

        return responseResult;
    }

    @Override
    public ResponseResult updateScore(Score scr) {

        ResponseResult responseResult = new ResponseResult();
        try {
             Integer line =  scoreDao.editScore(scr);
             if (line==0){
                 responseResult.setCode(110);
                 responseResult.setMsg("数据库中的数据已被修改,请刷新重试!");
             }else {
                 responseResult.setCode(0);
                 responseResult.setMsg("操作成功");
             }
        }catch (Exception e){
            responseResult.setCode(110);
            responseResult.setMsg("操作失败");
        }

        return responseResult;
    }

    @Override
    public ResponseResult batchDel(List<Integer> intIds) {

        ResponseResult responseResult = new ResponseResult();

        intIds.forEach(id->{
            try {
                scoreDao.deleteById(id);
                responseResult.setCode(0);
                responseResult.setMsg("操作成功");
            }catch (Exception e){
                e.printStackTrace();
                responseResult.setCode(110);
                responseResult.setMsg("操作失败");
            }
        });

        return responseResult;
    }

}
